//-----------------------------------------------------------------------------
//  
//  Copyright (c) 2013
//
//  Project  : 
//  Module   : 
//  Parent   : 
//
//  Description: 
//
//
//  Notes       : 
//


module BKP_MPI(
   input                  GTM_RESET,

   input                  GMPI_CLK,
   input[7:0]             GMPI_A,
   input[15:0]            GMPI_WD,
   input                  GMPI_WE,
   input                  GMPI_CS,
   output reg[15:0]       GMPI_RD_BKP,

   output                 MPI_OUT_GTXQ0_QPLLRESET,
   output                 MPI_OUT_GTXQ1_QPLLRESET,
   input                  MPI_IN_GTXQ0_QPLLLOCK,
   input                  MPI_IN_GTXQ1_QPLLLOCK,
   input                  MPI_IN_GTXQ0_QPLLREFCLKLOST,
   input                  MPI_IN_GTXQ1_QPLLREFCLKLOST,

   output[3:0]            MPI_OUT_GTXQ0_RESET,
   output[3:0]            MPI_OUT_GTXQ1_RESET,
   input[3:0]             MPI_IN_GTXQ0_RX_RESET_DONE,
   input[3:0]             MPI_IN_GTXQ1_RX_RESET_DONE,
   input[3:0]             MPI_IN_GTXQ0_TX_RESET_DONE,
   input[3:0]             MPI_IN_GTXQ1_TX_RESET_DONE,


   output[2:0]            MPI_OUT_GTXQ0_LOOPBACK_0,
   output[2:0]            MPI_OUT_GTXQ0_LOOPBACK_1,
   output[2:0]            MPI_OUT_GTXQ0_LOOPBACK_2,
   output[2:0]            MPI_OUT_GTXQ0_LOOPBACK_3,
   output[2:0]            MPI_OUT_GTXQ1_LOOPBACK_0,
   output[2:0]            MPI_OUT_GTXQ1_LOOPBACK_1,
   output[2:0]            MPI_OUT_GTXQ1_LOOPBACK_2,
   output[2:0]            MPI_OUT_GTXQ1_LOOPBACK_3
    ) ;


reg                       MPI_GTXQ0_QPLLRESET, MPI_GTXQ1_QPLLRESET;
reg[3:0]                  MPI_GTXQ0_RESET, MPI_GTXQ1_RESET;

reg[2:0]                  MPI_GTXQ0_LOOPBACK_0, MPI_GTXQ0_LOOPBACK_1, MPI_GTXQ0_LOOPBACK_2, MPI_GTXQ0_LOOPBACK_3;
reg[2:0]                  MPI_GTXQ1_LOOPBACK_0, MPI_GTXQ1_LOOPBACK_1, MPI_GTXQ1_LOOPBACK_2, MPI_GTXQ1_LOOPBACK_3;

reg[3:0]                  MPI_GTXQ0_RX_RESET_DONE, MPI_GTXQ1_RX_RESET_DONE;
reg[3:0]                  MPI_GTXQ0_TX_RESET_DONE, MPI_GTXQ1_TX_RESET_DONE;
reg                       MPI_GTXQ0_QPLLLOCK, MPI_GTXQ1_QPLLLOCK;
reg                       MPI_GTXQ0_QPLLREFCLKLOST, MPI_GTXQ1_QPLLREFCLKLOST;




// clock in the signals to GMPI_CLK
always @(posedge GTM_RESET or posedge GMPI_CLK) begin
   if (GTM_RESET==1'b1) begin
      MPI_GTXQ0_RX_RESET_DONE[3:0]                       <= 4'd0;
      MPI_GTXQ1_RX_RESET_DONE[3:0]                       <= 4'd0;
      MPI_GTXQ0_TX_RESET_DONE[3:0]                       <= 4'd0;
      MPI_GTXQ1_TX_RESET_DONE[3:0]                       <= 4'd0;
      MPI_GTXQ0_QPLLLOCK                                 <= 1'b0;
      MPI_GTXQ1_QPLLLOCK                                 <= 1'b0;
      MPI_GTXQ0_QPLLREFCLKLOST                           <= 1'b0;
      MPI_GTXQ1_QPLLREFCLKLOST                           <= 1'b0;
   end
   else begin
      MPI_GTXQ0_RX_RESET_DONE[3:0]                       <= MPI_IN_GTXQ0_RX_RESET_DONE[3:0];
      MPI_GTXQ1_RX_RESET_DONE[3:0]                       <= MPI_IN_GTXQ1_RX_RESET_DONE[3:0];
      MPI_GTXQ0_TX_RESET_DONE[3:0]                       <= MPI_IN_GTXQ0_TX_RESET_DONE[3:0];
      MPI_GTXQ1_TX_RESET_DONE[3:0]                       <= MPI_IN_GTXQ1_TX_RESET_DONE[3:0];
      MPI_GTXQ0_QPLLLOCK                                 <= MPI_IN_GTXQ0_QPLLLOCK;
      MPI_GTXQ1_QPLLLOCK                                 <= MPI_IN_GTXQ1_QPLLLOCK;
      MPI_GTXQ0_QPLLREFCLKLOST                           <= MPI_IN_GTXQ0_QPLLREFCLKLOST;
      MPI_GTXQ1_QPLLREFCLKLOST                           <= MPI_IN_GTXQ1_QPLLREFCLKLOST;
   end
end



always @(posedge GTM_RESET or posedge GMPI_CLK) begin
   if (GTM_RESET==1'b1) begin
      MPI_GTXQ0_QPLLRESET                                <= 1'b0;
      MPI_GTXQ1_QPLLRESET                                <= 1'b0;

      MPI_GTXQ0_RESET[3:0]                               <= 4'd0;
      MPI_GTXQ1_RESET[3:0]                               <= 4'h0;


      MPI_GTXQ0_LOOPBACK_0[2:0]                          <= 3'b000;
      MPI_GTXQ0_LOOPBACK_1[2:0]                          <= 3'b000;
      MPI_GTXQ0_LOOPBACK_2[2:0]                          <= 3'b000;
      MPI_GTXQ0_LOOPBACK_3[2:0]                          <= 3'b000;
      MPI_GTXQ1_LOOPBACK_0[2:0]                          <= 3'b000;
      MPI_GTXQ1_LOOPBACK_1[2:0]                          <= 3'b000;
      MPI_GTXQ1_LOOPBACK_2[2:0]                          <= 3'b000;
      MPI_GTXQ1_LOOPBACK_3[2:0]                          <= 3'b000;
   end
   else begin
      if (GMPI_WE==1'b1 && GMPI_CS==1'b1) begin
         case (GMPI_A[7:0])
         8'h40: MPI_GTXQ0_QPLLRESET                      <= GMPI_WD[0];
         8'h41: MPI_GTXQ0_RESET[3:0]                     <= GMPI_WD[3:0];
         8'h44: MPI_GTXQ0_LOOPBACK_0[2:0]                <= GMPI_WD[2:0];
         8'h45: MPI_GTXQ0_LOOPBACK_1[2:0]                <= GMPI_WD[2:0];
         8'h46: MPI_GTXQ0_LOOPBACK_2[2:0]                <= GMPI_WD[2:0];
         8'h47: MPI_GTXQ0_LOOPBACK_3[2:0]                <= GMPI_WD[2:0];
         8'h50: MPI_GTXQ1_QPLLRESET                      <= GMPI_WD[0];
         8'h51: MPI_GTXQ1_RESET[3:0]                     <= GMPI_WD[3:0];
         8'h54: MPI_GTXQ1_LOOPBACK_0[2:0]                <= GMPI_WD[2:0];
         8'h55: MPI_GTXQ1_LOOPBACK_1[2:0]                <= GMPI_WD[2:0];
         8'h56: MPI_GTXQ1_LOOPBACK_2[2:0]                <= GMPI_WD[2:0];
         8'h57: MPI_GTXQ1_LOOPBACK_3[2:0]                <= GMPI_WD[2:0];
         default: ;
         endcase
      end
   end
end
  assign  MPI_OUT_GTXQ0_QPLLRESET     = MPI_GTXQ0_QPLLRESET;
  assign  MPI_OUT_GTXQ1_QPLLRESET     = MPI_GTXQ1_QPLLRESET;

  assign  MPI_OUT_GTXQ0_RESET[3:0]    = MPI_GTXQ0_RESET[3:0];
  assign  MPI_OUT_GTXQ1_RESET[3:0]    = MPI_GTXQ1_RESET[3:0];

  assign  MPI_OUT_GTXQ0_LOOPBACK_0[2:0]   = MPI_GTXQ0_LOOPBACK_0[2:0];
  assign  MPI_OUT_GTXQ0_LOOPBACK_1[2:0]   = MPI_GTXQ0_LOOPBACK_1[2:0];
  assign  MPI_OUT_GTXQ0_LOOPBACK_2[2:0]   = MPI_GTXQ0_LOOPBACK_2[2:0];
  assign  MPI_OUT_GTXQ0_LOOPBACK_3[2:0]   = MPI_GTXQ0_LOOPBACK_3[2:0];
  assign  MPI_OUT_GTXQ1_LOOPBACK_0[2:0]   = MPI_GTXQ1_LOOPBACK_0[2:0];
  assign  MPI_OUT_GTXQ1_LOOPBACK_1[2:0]   = MPI_GTXQ1_LOOPBACK_1[2:0];
  assign  MPI_OUT_GTXQ1_LOOPBACK_2[2:0]   = MPI_GTXQ1_LOOPBACK_2[2:0];
  assign  MPI_OUT_GTXQ1_LOOPBACK_3[2:0]   = MPI_GTXQ1_LOOPBACK_3[2:0];

always @(posedge GTM_RESET or posedge GMPI_CLK) begin
   if (GTM_RESET==1'b1)
      GMPI_RD_BKP[15:0]                                  <= 16'd0;
   else begin
      if ( GMPI_CS==1'b1 ) begin
         case (GMPI_A[7:0])
         8'h00: GMPI_RD_BKP[15:0]                           <= 16'h000A;
         8'h40: begin
            GMPI_RD_BKP[15:0]                               <= 16'd0;
            GMPI_RD_BKP[5]                                  <= MPI_GTXQ0_QPLLREFCLKLOST;
            GMPI_RD_BKP[4]                                  <= MPI_GTXQ0_QPLLLOCK;
            GMPI_RD_BKP[0]                                  <= MPI_GTXQ0_QPLLRESET;
         end
         8'h41: GMPI_RD_BKP[15:0]                           <= {MPI_GTXQ0_RX_RESET_DONE[3:0], MPI_GTXQ0_TX_RESET_DONE[3:0], 4'd0, MPI_GTXQ0_RESET[3:0]};
         8'h44: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ0_LOOPBACK_0[2:0]};
         8'h45: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ0_LOOPBACK_1[2:0]};
         8'h46: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ0_LOOPBACK_2[2:0]};
         8'h47: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ0_LOOPBACK_3[2:0]};
//       8'h48: GMPI_RD_BKP[15:0]                           <= {12'd0, HSA_GTSITE0_RX_OOF[3:0]};

         8'h50:begin
            GMPI_RD_BKP[15:0]                               <= 16'd0;
            GMPI_RD_BKP[5]                                  <= MPI_GTXQ1_QPLLREFCLKLOST;
            GMPI_RD_BKP[4]                                  <= MPI_GTXQ1_QPLLLOCK;
            GMPI_RD_BKP[0]                                  <= MPI_GTXQ1_QPLLRESET;
         end
         8'h51: GMPI_RD_BKP[15:0]                           <= {MPI_GTXQ1_RX_RESET_DONE[3:0], MPI_GTXQ1_TX_RESET_DONE[3:0], 4'd0, MPI_GTXQ1_RESET[3:0]};
         8'h54: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ1_LOOPBACK_0[2:0]};
         8'h55: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ1_LOOPBACK_1[2:0]};
         8'h56: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ1_LOOPBACK_2[2:0]};
         8'h57: GMPI_RD_BKP[15:0]                           <= {13'd0, MPI_GTXQ1_LOOPBACK_3[2:0]};
//       8'h58: GMPI_RD_BKP[15:0]                           <= {12'd0, HSA_GTSITE1_RX_OOF[3:0]};

         default: GMPI_RD_BKP[15:0]                         <= 16'd0;
         endcase
      end
      else begin
                GMPI_RD_BKP[15:0]                           <= 16'd0;
      end
   end
end

endmodule
